package edu.mobbuzz.web.pages;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.Retain;
import org.apache.tapestry5.beaneditor.BeanModel;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.BeanModelSource;

import edu.mobbuzz.daf.bean.Rss;
import edu.mobbuzz.daf.bean.User;
import edu.mobbuzz.web.base.SecureBasePage;
import edu.mobbuzz.web.facade.ManagementFacade;

public class RssGridPage extends SecureBasePage{
	private ManagementFacade managementFacade;
	private List<Rss> rssList;
	@Property
	private Rss rss;
	
	@Retain
	private BeanModel rssModel;
	@Inject
	private BeanModelSource beanModelSource;
	@Inject
	private ComponentResources resources;
	
	@Component
	private Form rssForm;
	
	private boolean delete;
	private List<String> deletedList;
	
	public boolean isDelete() {
		return delete;
	}
	
	public void setDelete(boolean delete) {
		if(delete){
			getDeletedList().add(rss.getUrl());
		}
		this.delete = delete;
	}	
	
	public List<String> getDeletedList() {
		if(deletedList == null){
			deletedList = new ArrayList<String>();
		}
		return deletedList;
	}
	
	public ManagementFacade getManagementFacade() {
		if(managementFacade==null){
			managementFacade = new ManagementFacade();
		}
		return managementFacade;
	}

	public BeanModel getRssModel() {
		return rssModel;
	}	
	
	public List<Rss> getRssList() {
		if(rssList==null){
			rssList = new ArrayList<Rss>();
		}
		rssList = getManagementFacade().getRssList("", "");		
		return rssList;
	}

	public void setRssList(List<Rss> rssList) {
		this.rssList = rssList;
	}

	void pageLoaded(){
		rssModel = beanModelSource.createDisplayModel(Rss.class, resources.getMessages());
		rssModel.add("actions",null).label("actions");
	}
	
	Object onSuccessFromRssForm(){		
		for(String element : getDeletedList()){
			rss = (Rss)getManagementFacade().getRssByPrimaryKey(element);	
			if(rss!=null)
				getManagementFacade().removeRss(rss);
		}		
		return RssGridPage.class;
	}	
}
